<?php

namespace app\common\model;

use think\Model;

class Attachment extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 定义字段类型
    protected $type = [
    ];
    protected $append = [
        'thumb_style'
    ];

    public function setUploadtimeAttr($value)
    {
        return is_numeric($value) ? $value : strtotime($value);
    }

    /**
     * 获取云储存的缩略图样式字符
     */
    public function getThumbStyleAttr($value, $data)
    {
        if (!isset($data['storage']) || $data['storage'] == 'local') {
            return '';
        } else {
            $config = get_addon_config($data['storage']);
            if ($config && isset($config['thumbstyle'])) {
                return $config['thumbstyle'];
            }
        }
        return '';
    }

    public static function getMimetypeList()
    {
        $data = [
            "image/*"        => __("Image"),
            // "audio/*"        => __("Audio"),
            // "video/*"        => __("Video"),
            // "text/*"         => __("Text"),
            // "application/*"  => __("Application"),
            // "zip,rar,7z,tar" => __("Zip"),
        ];
        return $data;
    }

    protected static function init()
    {
        // 如果已经上传该资源，则不再记录
        self::beforeInsert(function ($model) {
            if (self::where('url', '=', $model['url'])->where('storage', $model['storage'])->find()) {
                return false;
            }
        });
    }
    // 获取媒体id
    public function getMedia_id($url,$wechat_id,$is_fullurl=false){
        $item = self::where(['url'=>$url,'wechat_id'=>$wechat_id])->find();
        if(!empty($item)){
            if($is_fullurl && !empty($item['wxurl'])){
                return $item['wxurl'];
            }
            // 媒体id
            if(!empty($item['media_id'])){
                return $item['media_id'];
            }
            $item = $item->toArray();
            // 上传到微信公众号
            $options =(new \app\common\model\Wechats)->getById($wechat_id);
            if(empty($options)){
                return '';
            }
            $Token_Access = (new \app\common\model\Wechats)->Token_Access($options);
            $weobj = WX($options,$Token_Access);
            $type = "image";
            if(in_array(strtolower($item['imagetype']), ['png','jpg','jpg'])){
                $type = "image";
            }
            $medias['media']=new \CURLFile(ROOT_PATH."public".DS.$item['url']);
            if($is_fullurl){
                $result = $weobj->uploadImageUrl($medias);
                if(isset($result['url'])){
                    self::where(['id'=>$item['id']])->update(['wxurl' => $result['url'],'is_wechat' => 2]);
                    return $result['url'];
                }
            }else{
                $result = $weobj->uploadMaterilaMedia($medias,$type);
                if(isset($result['media_id'])){
                    self::where(['id'=>$item['id']])->update(['media_id' => $result['media_id'],'wxurl' => $result['url'],'is_wechat' => 1]);
                    return $result['media_id'];
                }
            }
            return '';
        }
        // 空的
        $item = self::where(['url'=>$url])->find();
        if(!empty($item)){
            $item = $item->toArray();
           
            $item['wechat_id'] = $wechat_id;
            // 上传到微信公众号
            $options =(new \app\common\model\Wechats)->getById($wechat_id);
            if(empty($options)){
                return '';
            }
            $Token_Access = (new \app\common\model\Wechats)->Token_Access($options);
            $weobj = WX($options,$Token_Access);
            $type = "image";
            if(in_array(strtolower($item['imagetype']), ['png','jpg','jpg'])){
                $type = "image";
            }
            $medias['media']=new \CURLFile(ROOT_PATH."public".DS.$item['url']);
            $return = '';
            if($is_fullurl){
                $result = $weobj->uploadImageUrl($medias);
                $is_wechat = 2;
                $return = isset($result['url']) ? $result['url'] : '';
            }else{
                $result = $weobj->uploadMaterilaMedia($medias,$type);
                $is_wechat = 1;
                $return = isset($result['media_id']) ? $result['media_id'] : '';
            }
            
            if(isset($result['media_id']) || isset($result['url'])){
                $item['media_id'] = isset($result['media_id']) ? $result['media_id'] : '';
                $item['wxurl'] = isset($result['url']) ? $result['url'] : '';
                $item['is_wechat'] = $is_wechat;
                unset($item['thumb_style'],$item['id']);
                self::insert($item);
                return $return;
            }
            
        }
        return '';
    }
}
